package com.sikaryofficial.ai.controller;

/**
 * @author : wuweihong
 * @desc : TODO  请填写你的功能描述
 * @date : 2025-11-04
 */


import com.sikaryofficial.ai.service.RagService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.io.IOException;

@RestController
@RequestMapping("/rag")
@RequiredArgsConstructor
public class RagController {

	private final RagService ragService;

	@PostMapping("/init")
	public String initKnowledgeBase(@RequestParam String filePath) throws IOException {
		ragService.initKnowledgeBase(filePath);
		return "知识库初始化完成";
	}

	@GetMapping("/mcp-query")
	public Flux<String> getMcpAnswer(@RequestParam("query") String query) {
		return ragService.stream(query);
	}

	@GetMapping("generate/conversationId")
	public String generateConversationId() {
		return ragService.getConversationId();
	}

	//http://localhost:8088/rag/query?query=文档中所有内容
	@GetMapping("/query")
	public String processQuery(String query) {
		return ragService.processQuery(query);
	}

	//http://localhost:8088/rag/stream-query?query=文档中所有内容
	@GetMapping("/stream-query")
	public SseEmitter streamProcessQuery(@RequestParam("query") String query, @RequestParam("conversationId") String conversationId, HttpServletResponse response) {
		SseEmitter emitter = new SseEmitter(60000L);
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Connection", "keep-alive");
		response.setCharacterEncoding("UTF-8");
		return ragService.streamProcessQuery(query, conversationId, emitter);
	}
}
